package com.example.myapplication.shortvideo.fragment;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;

import com.example.myapplication.R;
import com.example.myapplication.databinding.FragmentCommentInputBinding;


/**
 * @author: rongfeng
 * @date: 2020/6/17
 * @description
 */
public class VideoCommentInputFragment extends DialogFragment {

    private InputMethodManager mInputMethodManager;
    FragmentCommentInputBinding binding ;

    public static VideoCommentInputFragment getInstance() {
        Bundle bundle = new Bundle();
        VideoCommentInputFragment fragment = new VideoCommentInputFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_comment_input, container, false);


          binding = DataBindingUtil.bind(view);


        getDialog().getWindow().setGravity(Gravity.BOTTOM);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        mInputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        setSoftKeyboard();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.1f;
        window.setAttributes(windowParams);
        getDialog().getWindow().setLayout(-1, -2);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }



    private void setSoftKeyboard() {
        binding.etInput.setFocusable(true);
        binding. etInput.setFocusableInTouchMode(true);
        binding.  etInput.requestFocus();

        //为 commentEditText 设置监听器，在 DialogFragment 绘制完后立即呼出软键盘，呼出成功后即注销
        binding. etInput.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mInputMethodManager != null) {
                    if (mInputMethodManager.showSoftInput(  binding.etInput, 0)) {
                        binding.  etInput.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                }
            }
        });

    }

}
